

# Proposition 27 

# Data preparation ----

gc()

## Set wd and load data
setwd("")
load("panel_us.RData")

## Load packages
library(sjmisc)
library(tidyverse)
source("function_plot_agr.R")

# Take a glimpse, select variables of interest, remove labels, frequencies
glimpse(cali)

df <- cali %>% select(vint_prop27_w2, vchoice_prop27_w2, particip_w2,
                      party_cue, pid_close_w1, pid_first_w1, pid_remind_w1, pid_remind_first_w1,
                      imp_prop27_w2, complex_prop27_w2,
                      genknow_a_w1, genknow_b_w1, genknow_c_w1, genknow_d_w1,  
                      arg_prop27_a_w2, arg_prop27_b_w2, arg_prop27_c_w2,
                      arg_prop27_d_w2, arg_prop27_e_w2, arg_prop27_f_w2, 
                      party_reco_prop27_w2, gov_reco_prop27_w2, 
                      intgrp_cniga_reco_prop27_w2, intgrp_cta_reco_prop27_w2,
                      intgrp_tribe_reco_prop27_w2, intgrp_drking_reco_prop27_w2,
                      day_itw_w2, month_itw_w2, isknow_prop27_c_w2, isknow_prop27_d_w2, 
                      starts_with("att"), -att_campaign_w3,
                      gender, age, educ_w1, county_w1, received_w2, already_vote_w2)
                      
df <- labelled::remove_labels(df)


## Regions ----

assign_region <- function(county) {
  southern_counties <- c("RIVERSIDE", "SAN DIEGO", "LOS ANGELES", "SAN BERNARDINO", "ORANGE", "VENTURA", "KERN", "IMPERIAL")
  
  if (county %in% southern_counties) {
    return("Southern California")
  } else {
    return("Northern California")
  }
}

df$region <- sapply(df$county_w1, assign_region)

df %>% select(county_w1, region) %>% print(n=500)
frq(df$region)

rm(assign_region)

## Gender ----
frq(df$gender)
df <- df %>% filter(gender == "male" | gender == "female")
df$gender <- droplevels(df$gender)
frq(df$gender)


## Vote intention or choice ----
frq(df$vint_prop27_w2)
frq(df$vchoice_prop27_w2)

df <- df %>%
  mutate(votew2 = case_when(
    vint_prop27_w2 == "certainly favor" | vint_prop27_w2 == "likely favor" | vchoice_prop27_w2 == "yes" ~ "yes",
    vint_prop27_w2 == "certainly against" | vint_prop27_w2 == "likely against" | vchoice_prop27_w2 == "no" ~ "no",
    vint_prop27_w2 == "dk" | vchoice_prop27_w2 == "dk" ~ "dk"),
         vote_w2_int_choice = case_when(
    vint_prop27_w2 == "certainly favor" | vint_prop27_w2 == "likely favor" ~ "int yes",
    vchoice_prop27_w2 == "yes" ~ "choice yes",
    vint_prop27_w2 == "certainly against" | vint_prop27_w2 == "likely against" ~ "int no",
    vchoice_prop27_w2 == "no" ~ "choice no",
    vint_prop27_w2 == "dk" | vchoice_prop27_w2 == "dk" ~ "dk"))

frq(df$votew2)
xtabs(~ vint_prop27_w2 + votew2, data = df, subset = NULL)
xtabs(~ vchoice_prop27_w2 + votew2, data = df, subset = NULL)

frq(df$vote_w2_int_choice)
xtabs(~ vint_prop27_w2 + vote_w2_int_choice, data = df, subset = NULL)
xtabs(~ vchoice_prop27_w2 + vote_w2_int_choice, data = df, subset = NULL)


## Merge day / week of itw ----
frq(df$day_itw_w2)
frq(df$month_itw_w2)

xtabs(~ day_itw_w2 + month_itw_w2, data = df, subset = NULL)

df <- df %>% mutate(date_itw = paste(as.character(day_itw_w2), as.character(month_itw_w2), "2022", sep = "-"))

df$date_itw <- as.Date(df$date_itw, format = "%d-%m-%Y")

frq(df$date_itw)

df <- df %>% mutate(days = 41-(as.numeric(difftime(as.Date("2022-11-08"), date_itw, units = "days"))))

frq(df$days)
xtabs(~ date_itw + days, data = df, subset = NULL)

### Number of observations / each day 
freq_table <- as.data.frame(table(df$days, useNA = "no"))
N_values <- freq_table$Freq
mean(N_values)
sd(N_values)

rm(freq_table, N_values)


## Already voted or not
xtabs(~ already_vote_w2 + received_w2, data = df, addNA = T, subset = NULL)

df <- df %>%
  mutate(received_w2 = case_when(
    received_w2 == 1 ~ "yes",
    received_w2 == 2 ~ "no",
    received_w2 == 99 ~ "nsp",
    TRUE ~ NA), 
    already_vote_w2 = case_when(
      already_vote_w2 == 1 ~ "yes",
      already_vote_w2 == 2 ~ "no",
      TRUE ~ NA),
    votemail = case_when(
      received_w2 %in% c("no","nsp") ~ "not receive",
      received_w2 == "yes" & already_vote_w2 == "no" ~ "receive not vote",
      received_w2 == "yes" & already_vote_w2 == "yes" ~ "receive and vote",
      TRUE ~ NA))

xtabs(~ already_vote_w2 + received_w2, data = df, addNA = T, subset = NULL)
xtabs(~ already_vote_w2 + votemail, data = df, addNA = T, subset = NULL)
xtabs(~ votemail + received_w2, data = df, addNA = T, subset = NULL)
frq(df$votemail)
xtabs(~ votemail + vote_w2_int_choice, data = df, addNA = T, subset = NULL)

df <- df %>% mutate(filter_particip_profile = 
                      case_when(votemail == "receive and vote" | particip_w2 == "certainly yes" ~ 1,
                                is.na(days) ~ NA,
                                TRUE ~ 0))
frq(df$filter_particip_profile)
df %>% select(days, received_w2, already_vote_w2, particip_w2, votemail, vote_w2_int_choice, votew2, filter_particip_profile) %>% print(n = 50)




## Arguments ----
col_names <- c("arg_prop27_a_w2", "arg_prop27_b_w2", "arg_prop27_c_w2", "arg_prop27_d_w2", "arg_prop27_e_w2", "arg_prop27_f_w2")

df <- df %>%
  mutate(across(c(arg_prop27_a_w2, arg_prop27_b_w2, arg_prop27_c_w2),
                  ~ case_when(. == "strong agree" ~ 2, . == "agree" ~ 1,
                              . == "dk" ~ 0,
                              . == "disagree" ~ -1, . == "strong disagree" ~ -2),
                              .names = "{col}_rcd")) %>% 
  mutate(across(c(arg_prop27_d_w2, arg_prop27_e_w2, arg_prop27_f_w2),
                  ~ case_when(. == "strong agree" ~ -2, . == "agree" ~ -1,
                              . == "dk" ~ 0,
                              . == "disagree" ~ 1, . == "strong disagree" ~ 2),
                              .names = "{col}_rcd")) %>% 
  mutate(arg_pro_prop27_w2 = arg_prop27_a_w2_rcd + arg_prop27_b_w2_rcd + arg_prop27_c_w2_rcd,
         arg_con_prop27_w2 = arg_prop27_d_w2_rcd + arg_prop27_e_w2_rcd + arg_prop27_f_w2_rcd,
         arg_full_prop27_w2 = arg_pro_prop27_w2 + arg_con_prop27_w2) %>% 
  mutate(arg_n_dk = case_when(rowSums(!is.na(across(all_of(col_names)))) == 0 ~ NA_real_,
                              T ~ rowSums(across(all_of(col_names)) == "dk", na.rm = T)))

rm(col_names)


## PID ----
df %>% select(starts_with("pid")) %>% frq()
xtabs(~ pid_close_w1 + pid_first_w1, data = df, subset = NULL)
xtabs(~ pid_close_w1 + pid_remind_w1, data = df, subset = NULL)
xtabs(~ pid_close_w1 + pid_remind_first_w1, data = df, subset = NULL)
xtabs(~ pid_remind_w1 + pid_remind_first_w1, data = df, subset = NULL)
xtabs(~ pid_first_w1 + pid_remind_first_w1, data = df, subset = NULL)


df <- df %>% mutate(pid = 
                      case_when(pid_first_w1 == "Democrat" | pid_remind_first_w1 == "Democrat" ~ "Democrat",
                                pid_first_w1 == "Republican" | pid_remind_first_w1 == "Republican" ~ "Republican",
                                pid_remind_first_w1 == "dk" ~ "noPID",
                                pid_close_w1 == "no" & (pid_remind_w1 == "no" | pid_remind_w1 == "dk") ~ "noPID",
                                pid_close_w1 == "dk" & (pid_remind_w1 == "no" | pid_remind_w1 == "dk") ~ "noPID",
                                pid_close_w1 == "yes" & pid_remind_w1 == "no" ~ "noPID"))

frq(df$pid)
df %>% select(starts_with("pid")) %>% print(n=50)



## Knowledge party cues ----
frq(df$pid)
df <- df %>% filter(pid %in% c("Democrat","Republican","noPID"))
frq(df$pid)

frq(df$party_reco_prop27_w2) 
xtabs(~ pid + party_reco_prop27_w2, data = df, subset = NULL)
addmargins(xtabs(~ pid + party_reco_prop27_w2, data = df, subset = NULL))

df <- df %>% 
  mutate(party_reco_prop27_w2_know = case_when(
    pid == "Republican" & party_reco_prop27_w2 == "no" ~ "correct",
    pid == "Democrat" & party_reco_prop27_w2 == "no" ~ "correct",
    pid == "Republican" & party_reco_prop27_w2 %in% c("yes","free","dk") ~ "not",
    pid == "Democrat" & party_reco_prop27_w2 %in% c("yes","free","dk") ~ "not",
    pid == "noPID" ~ "noPID"))

frq(df$party_reco_prop27_w2_know) 


# Group "free" with "dk" (knowledge of cues)
df <- df %>% 
  mutate(across(c(party_reco_prop27_w2),
                ~ case_when(. == "yes" ~ "yes",
                            . == "no" ~ "no",
                            . == "free" | . == "dk" ~ "free/dk"),
                .names = "{col}_rcd"))

frq(df$party_reco_prop27_w2_rcd)


## Importance ----
frq(df$imp_prop27_w2)
df$imp_prop27_w2[df$imp_prop27_w2 == 99] <- NA
frq(df$imp_prop27_w2)

df <- df %>% mutate(imp_prop27_w2_bi = case_when(
  imp_prop27_w2 < 6 ~ 0,
  imp_prop27_w2 >= 6 ~ 1,
  TRUE ~ NA))  

xtabs(~ imp_prop27_w2 + imp_prop27_w2_bi, data = df, subset = NULL)


## IS knowledge ----
frq(df$isknow_prop27_c_w2)
frq(df$isknow_prop27_d_w2)

df <- df %>%
  mutate(isknow_prop27_c_w2_rcd = ifelse(is.na(isknow_prop27_c_w2), NA, as.numeric(grepl("correct", tolower(isknow_prop27_c_w2)))),
         isknow_prop27_d_w2_rcd = ifelse(is.na(isknow_prop27_d_w2), NA, as.numeric(grepl("correct", tolower(isknow_prop27_d_w2)))))

frq(df$isknow_prop27_c_w2_rcd)
frq(df$isknow_prop27_d_w2_rcd)

df <- df %>% mutate(isknow_prop27_scale = isknow_prop27_c_w2_rcd + isknow_prop27_d_w2_rcd) %>% 
             mutate(isknow_prop27_scale_bi = case_when(isknow_prop27_scale == 0 ~ 0,
                                                       isknow_prop27_scale == 1 | isknow_prop27_scale == 2 ~ 1))
             
frq(df$isknow_prop27_scale)
frq(df$isknow_prop27_scale_bi)



## Select var and remove NA ----

df <- df %>% 
  select(isknow_prop27_scale, party_reco_prop27_w2_know, arg_n_dk, arg_full_prop27_w2, votemail, filter_particip_profile, 
         date_itw, days, votew2, pid, imp_prop27_w2, gender, age, educ_w1,
         arg_prop27_a_w2_rcd, arg_prop27_b_w2_rcd, arg_prop27_c_w2_rcd,
         arg_prop27_d_w2_rcd, arg_prop27_e_w2_rcd, arg_prop27_f_w2_rcd) %>% 
  drop_na()


## Recode mediators/outcomes ----

### Consistent voting ----
df <- df %>% mutate(
  voteinlinearg = case_when(votew2 == "dk" ~ "undecided",
                            arg_full_prop27_w2 <= -3 & votew2 == "no" ~ "inline",
                            arg_full_prop27_w2 <= -3 & votew2 == "yes" ~ "not inline",
                            arg_full_prop27_w2 >= -2 & arg_full_prop27_w2 <= 2 ~ "ambivalent",
                            arg_full_prop27_w2 >= 3 & votew2 == "no" ~ "not inline",
                            arg_full_prop27_w2 >= 3 & votew2 == "yes" ~ "inline"),
  voteinlinearg0 = case_when(votew2 == "dk" ~ "undecided",
                             arg_full_prop27_w2 <= -1 & votew2 == "no" ~ "inline",
                             arg_full_prop27_w2 <= -1 & votew2 == "yes" ~ "not inline",
                             arg_full_prop27_w2 == 0 ~ "ambivalent",
                             arg_full_prop27_w2 >= 1 & votew2 == "no" ~ "not inline",
                             arg_full_prop27_w2 >= 1 & votew2 == "yes" ~ "inline"),
  voteinlinearg5 = case_when(votew2 == "dk" ~ "undecided",
                             arg_full_prop27_w2 <= -6 & votew2 == "no" ~ "inline",
                             arg_full_prop27_w2 <= -6 & votew2 == "yes" ~ "not inline",
                             arg_full_prop27_w2 >= -5 & arg_full_prop27_w2 <= 5 ~ "ambivalent",
                             arg_full_prop27_w2 >= 6 & votew2 == "no" ~ "not inline",
                             arg_full_prop27_w2 >= 6 & votew2 == "yes" ~ "inline"),
  voteinlinearg_rcd = case_when(voteinlinearg == "inline" ~ 1,
                                voteinlinearg %in% c("ambivalent","not inline","undecided") ~ 0),
  voteinlinearg0_rcd = case_when(voteinlinearg0 == "inline" ~ 1,
                                 voteinlinearg0 %in% c("ambivalent","not inline","undecided") ~ 0),
  voteinlinearg5_rcd = case_when(voteinlinearg5 == "inline" ~ 1,
                                 voteinlinearg5 %in% c("ambivalent","not inline","undecided") ~ 0))



### Knowledge cue and vote inline ----
df <- df %>% mutate(party_reco_prop27_w2_know_rcd = case_when(party_reco_prop27_w2_know == "correct" ~ 1,
                                                              party_reco_prop27_w2_know == "noPID" | party_reco_prop27_w2_know == "not" ~ 0),
                    voteinlineparty = case_when(pid == "Democrat" & votew2 == "yes" ~ "not inline",
                                                pid == "Democrat" & votew2 == "no" ~ "inline",
                                                pid == "Democrat" & votew2 == "dk" ~ "undecided",
                                                pid == "Republican" & votew2 == "yes" ~ "not inline",
                                                pid == "Republican" & votew2 == "no" ~ "inline",
                                                pid == "Republican" & votew2 == "dk" ~ "undecided",
                                                pid == "noPID" ~ "noPID"),
                    voteinlineparty_rcd = case_when(voteinlineparty == "inline" ~ 1,
                                                    voteinlineparty %in% c("not inline", "undecided", "noPID") ~ 0))



## Filter only party identifiers ---- 
df <- df %>% filter(pid != "noPID")
frq(df$pid)


## Descriptive / appendix A
frq(df$votemail) 
frq(df$filter_particip_profile) 

frq(df$party_reco_prop27_w2_know_rcd)

ggplot(df) + 
  aes(x = arg_full_prop27_w2) +
  geom_density(fill = "grey", alpha = 0.5) +
  theme_minimal() +
  scale_x_continuous(limits = c(-12, 12), breaks = seq(-12, 12, by = 2)) + 
  xlab("Proposition 27 - argument scale") +
  ylab("Density") 

ggsave("scale_prop27.png", scale = 1, width = 12, height = 6, units = "cm", dpi = 600)

avg_dk_prop27 <- mean(df$arg_n_dk)
round(avg_dk_prop27,2)
sd_dk_prop27 <- sd(df$arg_n_dk)
round(sd_dk_prop27,2)

frq(df$voteinlineparty_rcd)
frq(df$voteinlinearg_rcd)


addmargins(xtabs(~ voteinlineparty_rcd + voteinlinearg_rcd, data = df, subset = NULL))
cross_prop27 <- as.data.frame(questionr::prop(xtabs(~ voteinlineparty_rcd + voteinlinearg_rcd, data = df, subset = NULL)))
cross_prop27 <- cross_prop27 %>% mutate(ballot = "prop27")
cross_prop27




# Alpha arguments
df %>% 
  select(arg_prop27_a_w2_rcd, arg_prop27_b_w2_rcd, arg_prop27_c_w2_rcd) %>% 
  psych::alpha()  

df %>% 
  select(arg_prop27_d_w2_rcd, arg_prop27_e_w2_rcd, arg_prop27_f_w2_rcd) %>% 
  psych::alpha() 




# Analysis - aggregate ----

## 2a ----
cat_ols(df, "party_reco_prop27_w2_know_rcd", ballot = "prop27")

p01_prop27 <- graf

p01_prop27 <- p01_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "", title = "Proposition 27") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                                                       values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p01_prop27

magr <- lm(percentage ~ days * party_reco_prop27_w2_know_rcd, data = dfagr)
summary(magr)

magr_know_cue <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "party_reco_prop27_w2_know_rcd1" = "Knowledge party cue",
                       "days:party_reco_prop27_w2_know_rcd1" = "Days × Knowledge party cue"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 


## 2b ----
frq(df$arg_n_dk)

num_ols(df, "arg_n_dk", ballot = "prop27")
p02_prop27 <- graf

p02_prop27 <- p02_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = avg), method = "lm", alpha = 0.2, linewidth = 0.5, color = "black") +
  labs(x = "Days before voting day", y = "Average", color = "", fill = "", title = "Proposition 27") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(0, 2, by = 0.2), limits = c(0,2)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p02_prop27

magr <- lm(avg ~ days, data = dfagr)
summary(magr)

magr_dk <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term, "(Intercept)" = "Intercept", "days" = "Days"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig)  




## 2c ----
frq(df$voteinlineparty_rcd)

cat_ols(df, "voteinlineparty_rcd", ballot = "prop27")
p04_prop27 <- graf

p04_prop27 <- p04_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 27") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p04_prop27

magr <- lm(percentage ~ days * voteinlineparty_rcd, data = dfagr)
summary(magr)

magr_inline <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "voteinlineparty_rcd1" = "Inline",
                       "days:voteinlineparty_rcd1" = "Days × Inline"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 


## 2d ----
frq(df$voteinlinearg_rcd)

cat_ols(df, "voteinlinearg_rcd", ballot = "prop27")
p03_prop27 <- graf

p03_prop27 <- p03_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 27") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p03_prop27

magr <- lm(percentage ~ days * voteinlinearg_rcd, data = dfagr)
summary(magr)


magr_cons <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "voteinlinearg_rcd1" = "Consistent voting",
                       "days:voteinlinearg_rcd1" = "Days × Consistent voting"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 


## Appendix B1 ----
writexl::write_xlsx(list(
  "2a" = magr_know_cue, "2b" = magr_dk,
  "2c" = magr_inline, "2d" = magr_cons),
  "magr_prop27.xlsx")

rm(graf, dfagr, magr)



# Analysis - individual ---- 

glimpse(df)

colsss <- c("gender", "voteinlineparty_rcd", "voteinlinearg_rcd", "voteinlinearg0_rcd", "voteinlinearg5_rcd")
df[colsss] <- lapply(df[colsss], as.factor)

colsss <- c("party_reco_prop27_w2_know_rcd", "isknow_prop27_scale", "arg_n_dk", "days", "imp_prop27_w2", "age", "educ_w1")
df[colsss] <- lapply(df[colsss], as.numeric)
df[colsss] <- lapply(df[colsss], scales::rescale)

rm(colsss)


## Appendix B2 ----

library(lavaan)

glimpse(df)

df$party_reco_prop27_w2_know_rcd <- as.factor(df$party_reco_prop27_w2_know_rcd)
df$gender <- as.numeric(df$gender)


model <- '
  # Days -> cue -> vote inline
  party_reco_prop27_w2_know_rcd ~ a1*days + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_prop27_w2_know_rcd + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age  
  voteinlinearg_rcd ~ c2*days + b2*arg_n_dk + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_prop27_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_prop27 <- sem(model, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg_rcd","party_reco_prop27_w2_know_rcd"))

sumfit_prop27 <- summary(fit_prop27, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_prop27_df <- sumfit_prop27[["pe"]]




# Robustness ----

## Model for figure C3a.3 ----


model0 <- '
  # Days -> cue -> vote inline
  party_reco_prop27_w2_know_rcd ~ a1*days + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_prop27_w2_know_rcd + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age  
  voteinlinearg0_rcd ~ c2*days + b2*arg_n_dk + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_prop27_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg0_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_prop270 <- sem(model0, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg0_rcd","party_reco_prop27_w2_know_rcd"))

sumfit_prop270 <- summary(fit_prop270, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_prop270_df <- sumfit_prop270[["pe"]]


## Model for figure C3b.3 ----

model5 <- '
  # Days -> cue -> vote inline
  party_reco_prop27_w2_know_rcd ~ a1*days + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_prop27_w2_know_rcd + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age  
  voteinlinearg5_rcd ~ c2*days + b2*arg_n_dk + isknow_prop27_scale + imp_prop27_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_prop27_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg5_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_prop275 <- sem(model5, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg5_rcd","party_reco_prop27_w2_know_rcd"))

sumfit_prop275 <- summary(fit_prop275, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_prop275_df <- sumfit_prop275[["pe"]]



## Figure C3a.2d ----


frq(df$voteinlinearg0_rcd)

cat_ols(df, "voteinlinearg0_rcd", ballot = "prop27")
pzero_prop27 <- graf

pzero_prop27[["data"]][["days"]] <- pzero_prop27[["data"]][["days"]] * 40

pzero_prop27 <- pzero_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 27") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pzero_prop27

magr <- lm(percentage ~ days * voteinlinearg0_rcd, data = dfagr)
summary(magr)

rm(graf)



## Figure C3b.2d ----

frq(df$voteinlinearg5_rcd)

cat_ols(df, "voteinlinearg5_rcd", ballot = "prop27")
pfive_prop27 <- graf

pfive_prop27[["data"]][["days"]] <- pfive_prop27[["data"]][["days"]] * 40

pfive_prop27 <- pfive_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 27") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pfive_prop27

magr <- lm(percentage ~ days * voteinlinearg5_rcd, data = dfagr)
summary(magr)

rm(graf)



## Appendix C1 ----

df_certain_voter <- df %>% filter(filter_particip_profile == 1)

### Figure C1.2a ----

cat_ols(df_certain_voter, "party_reco_prop27_w2_know_rcd", ballot = "prop27")

pcertain01_prop27 <- graf

pcertain01_prop27[["data"]][["days"]] <- pcertain01_prop27[["data"]][["days"]] * 40

pcertain01_prop27 <- pcertain01_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 27") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain01_prop27

magr <- lm(percentage ~ days * party_reco_prop27_w2_know_rcd, data = dfagr)
summary(magr)



### Figure C1.2b ----
frq(df_certain_voter$arg_n_dk)

num_ols(df_certain_voter, "arg_n_dk", ballot = "prop27")
pcertain02_prop27 <- graf

pcertain02_prop27[["data"]][["days"]] <- pcertain02_prop27[["data"]][["days"]] * 40
pcertain02_prop27[["data"]][["avg"]] <- pcertain02_prop27[["data"]][["avg"]] * 6

pcertain02_prop27 <- pcertain02_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = avg), method = "lm", alpha = 0.2, linewidth = 0.5, color = "black") +
  labs(x = "Days before voting day", y = "Average", color = "", fill = "",
       title = "Proposition 27") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(0, 2, by = 0.2), limits = c(0,2)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain02_prop27

magr <- lm(avg ~ days, data = dfagr)
summary(magr)




### Figure C1.2c ----
frq(df_certain_voter$voteinlineparty_rcd)

cat_ols(df_certain_voter, "voteinlineparty_rcd", ballot = "prop27")
pcertain04_prop27 <- graf

pcertain04_prop27[["data"]][["days"]] <- pcertain04_prop27[["data"]][["days"]] * 40

pcertain04_prop27 <- pcertain04_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 27") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain04_prop27

magr <- lm(percentage ~ days * voteinlineparty_rcd, data = dfagr)
summary(magr)




### Figure C1.2d ----
frq(df_certain_voter$voteinlinearg_rcd)

cat_ols(df_certain_voter, "voteinlinearg_rcd", ballot = "prop27")
pcertain03_prop27 <- graf

pcertain03_prop27[["data"]][["days"]] <- pcertain03_prop27[["data"]][["days"]] * 40

pcertain03_prop27 <- pcertain03_prop27 +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Proposition 27") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain03_prop27

magr <- lm(percentage ~ days * voteinlinearg_rcd, data = dfagr)
summary(magr)

rm(graf)



### Results for figure C1.3b ----
fit_prop27certain <- sem(model, data = df_certain_voter, ordered = c("voteinlineparty_rcd","voteinlinearg_rcd","party_reco_prop27_w2_know_rcd"))

sumfit_prop27certain <- summary(fit_prop27certain, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_prop27certain_df <- sumfit_prop27certain[["pe"]]

rm(dfagr, magr, model, model0, model5)
   
   

